package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.Users;
import com.qf.mapper.UsersMapper;
import com.qf.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lenovo
 * @version V1.0
 * @Date 2021/10/5 21:56
 */
@Service
public class UserServiceImpl implements UsersService {

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public boolean loginByNameAndPassword(String name, String password) {
        List<Users> users = usersMapper.loginByNameAndPassword(name, password);
        if (users != null && users.size() > 0){
            return true;
        }
        return false;
    }

    @Override
    public List<Users> findAll() {
        return usersMapper.findAll();
    }

    @Override
    public List<Users> findAllByTypeId(Integer typeId) {
        return usersMapper.findAllByTypeId(typeId);
    }

    @Override
    public List<Users> findUserByName(String name) {
        return usersMapper.findUserByName(name);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void updatePassword(String name, String oldPassword, String newPassword) {
        boolean b = loginByNameAndPassword(name, oldPassword);

        if (b){
            usersMapper.updatePassword(name, newPassword);
        }else {
            System.out.println("修改失败，用户不存在");
            throw new RuntimeException("修改失败，用户不存在");
        }
    }

    @Override
    public PageInfo<Users> findAllByPage(Integer num, Integer size) {

        //1.开启分页
        PageHelper.startPage(num,size);

        //2.查询数据
        List<Users> list = usersMapper.findAll();

        //3.封装
        PageInfo<Users> pageInfo = new PageInfo<>(list);

        //4.返回
        return pageInfo;
    }
}
